﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestXNA3.Resources.Shaders;
using Microsoft.Xna.Framework;

namespace TestXNA3.Resources.Meshes
{
    public class Sprite : MeshResource
    {
        private SpriteDefaultShader _spriteShader;
        public TextureResource Texture
        {
            get { return _spriteShader.Diffuse; }
            set { _spriteShader.Diffuse = value; }
        }

        public Vector3 Size = Vector3.One;

        public Sprite(string textureName)
        {
            _spriteShader = new SpriteDefaultShader();
            Shader = _spriteShader;

            _spriteShader.Unlit = true;
            CastShadows = false;
        }

        public override void Render(ref Microsoft.Xna.Framework.Matrix view, ref Microsoft.Xna.Framework.Matrix proj, ref Microsoft.Xna.Framework.Vector3 ViewerLocation, TestXNA3.Render.SceneRenderer.EDrawMode drawMode)
        {
            if (drawMode == TestXNA3.Render.SceneRenderer.EDrawMode.DepthOnly)
                return;

            Matrix billboardMatrix = Matrix.Invert(view);

            billboardMatrix.M41 = World.Translation.X;
            billboardMatrix.M42 = World.Translation.Y;
            billboardMatrix.M43 = World.Translation.Z;

            Shader.World = Matrix.CreateScale(Size) * billboardMatrix;
            Shader.View = view;
            Shader.Projection = proj;
            Shader.ApplyShaderConst();

            Shader.Begin();
            int passCount = Shader.GetPassCount();
            for (int i = 0; i < passCount; i++)
            {
                Shader.BeginPass(i);
                GameStartup.QuadRenderer.Render3D(new Vector3(-1, -1, 0), new Vector3(1, 1, 0));
                Shader.EndPass(i);
            }
            Shader.End();
        }
    }
}
